package org.tulkas.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import org.junit.Test;
import org.tulkas.test.TulkasCase;
import org.tulkas.util.EncodeUtil;
import org.tulkas.util.FileUtil;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class GlobalConfigTest extends TulkasCase {
	@Test
	public void testDefault() throws Exception {
		GlobalConfig.destroyInstance();
		String usrDir = FileUtil.trimFilePath(System.getProperty("user.home"));
		InputStream is = this.getClass().getClassLoader().getResourceAsStream(
				"org/tulkas/util/test/res/test-tulkas-default.properties");
		new File(usrDir + "/.tulkas/conf").mkdirs();
		FileOutputStream fos = new FileOutputStream(usrDir + "/.tulkas/"
				+ GlobalConfig.CUSTOM_CONFIG_FILE);
		FileUtil.readAndWrite(is, fos);
		is.close();
		fos.close();

		GlobalConfig config = GlobalConfig.getInstance();
		assertEquals(usrDir + "/.tulkas", config.getHomePath());
		String[] keys = config.getAllKeys();
		assertTrue(keys.length > 10);

		assertEquals(true, config.getBoolean("client.options.cookie.enabled",
				false));
		assertEquals(1, config.getInt("flusher.manager.interval", 0));
		assertEquals(1l, config.getLong("flusher.manager.interval", 0l));
		assertEquals(0.1f, config.getFloat(
				"client.options.cache.heuristicCoefficient", 0.2f), 0.01f);
		assertEquals(0.1, config.getDouble(
				"client.options.cache.heuristicCoefficient", 0.2), 0.01);
		assertEquals("UTF-8", config.getString("client.options.defaultCharset",
				"GBK"));

		assertEquals(false, config.getBoolean("aaa", false));
		assertEquals(2, config.getInt("bbb", 2));
		assertEquals(33, config.getLong("ccc", 33l));
		assertEquals(0.2f, config.getFloat("ddd", 0.2f), 0.01f);
		assertEquals(0.2, config.getDouble("eee", 0.2), 0.01);
		assertEquals("GBK", config.getString("fff", "GBK"));
	}

	@Test
	public void testCustomConfigs() throws Exception {
		GlobalConfig.destroyInstance();
		String dirStr = System.getProperty("java.io.tmpdir");
		File rootDir = new File(dirStr, EncodeUtil.uuid());
		String rootPath = FileUtil.trimFilePath(rootDir.getAbsolutePath());

		GlobalConfig.destroyInstance();
		GlobalConfig.init(rootPath + "/data2");
		assertEquals(rootPath + "/data2", GlobalConfig.getInstance()
				.getHomePath());

		//		InputStream is = this.getClass().getClassLoader().getResourceAsStream(
		//				"org/tulkas/config/test-tulkas-default.properties");
		//		new File(rootPath + "/data/conf").mkdirs();
		//		FileOutputStream fos = new FileOutputStream(rootPath + "/data/"
		//				+ GlobalConfig.CUSTOM_CONFIG_FILE);
		//		FileUtil.readAndWrite(is, fos);
		//		is.close();
		//		fos.close();

		GlobalConfig.init(rootPath + "/data");
		assertEquals(rootPath + "/data", GlobalConfig.getInstance()
				.getHomePath());

		GlobalConfig config = GlobalConfig.getInstance();
		config.setValue("custom.key.key1", "111");
		config.setValue("custom.key.key2", "key2");
		config.setValue("custom.key.key3", "11.22");
		config.setValue("flusher.manager.interval", "2");
		assertEquals(true, config.containsKey("flusher.manager.interval"));

		assertEquals(3, config.getSubConfig("custom.key.").getAllKeys().length);
		assertEquals(3, config.getSubProperties("custom.key.").size());

		config.save();
		String file = rootPath + "/data/" + GlobalConfig.CUSTOM_CONFIG_FILE;
		assertTrue(new File(file).exists());

		Properties props = new Properties();
		FileInputStream fis = new FileInputStream(file);
		props.load(fis);
		assertEquals(4, props.size());

		config.remove("custom.key.key3");
		assertEquals(1.1, config.getDouble("custom.key.key3", 1.1), 0.001);

		config.clear();
		int c1 = config.getAllKeys().length;
		assertEquals(true, c1 > 0);

		config.load();
		int c2 = config.getAllKeys().length;
		assertEquals(3, c2 - c1);
	}
}
